﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElectronicCommerce.Framework.Reflection;
using ElectronicCommerce.Framework.ServiceModel.Configuration;
using ElectronicCommerce.Framework.Utility;

namespace ElectronicCommerce.Framework.ServiceModel
{
    /// <summary>
    /// 
    /// </summary>
    public class AssemblyServiceBroker : ServiceBrokerBase, IServiceBroker, IServiceProvider
    {
        private static IServiceBroker serviceBroker = new AssemblyServiceBroker();

        private AssemblyServiceBroker() { }

        public static IServiceBroker Instance
        {
            get
            {
                return serviceBroker;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public override object DoGetService(Type serviceType, string serviceName)
        {
            object service = null;

            ServiceElement serviceElement =
                ServiceModelConfigHelper.ServiceConfig.GetMixServiceElement(serviceType, serviceName);
            if (serviceElement != null)
            {
                Type classType = TypeHelper.ResolveType(serviceElement.ClassTypeName);
                service = MethodCaller.CreateInstance(classType);
            }

            return service;
        }
    }
}
